<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=750, user-scalable=no">
    <title>游农链</title>
    <link rel="stylesheet" type="text/css" href="{{asset('css/frontend/reset.css')}}" />
    <link rel="stylesheet" type="text/css" href="{{asset('css/frontend/yangshi.css')}}">
    <style>
    </style>
</head>
<body>

<div id="app">
    <img class="bg2" src="{{asset('images/bg2.png')}}" alt="">
    <div class="shenqing">
        <div class="cell">
            <img src="{{asset('images/tel.png')}}" class="inputBg" />
            <input type="text" id="tel" class="txt" placeholder="请填写手机号码" />
            <div class="clear"></div>
            <div class="err red" id="telErr"></div>
        </div>

        <div class="cell cellFlex">
            <div class="left">
                <img src="{{asset('images/code.png')}}" class="inputBg" />
                <input type="text" id="code" class="txt2" placeholder="请填写验证码" />
                <div class="clear"></div>
                <div class="err red" id="codeErr"></div>
            </div>

            <div class="right">
                <button id="getCode">获取验证码</button>
            </div>

        </div>

        <div class="cell">
            <img src="{{asset('images/clock.png')}}" class="inputBg" />
            <input type="text" id="pwd" class="txt" placeholder="请输入您的密码" />
            <div class="clear"></div>
            <div class="err red" id="pwdErr"></div>
        </div>
        <div class="cell inviter">
            <img src="{{asset('images/qrcode.png')}}" class="inputBg" />
            <input type="text" id="inviter" class="txt" placeholder="请填写邀请码" />
            <div class="clear"></div>
        </div>
        <div class="cell">
            <button id="ljsq">立即递交</button>
        </div>
    </div>
</div>

<script type="text/javascript" src="{{asset('js/frontend/jquery-3.3.1.min.js')}}"></script>
<script src="{{asset('js/frontend/commom.js')}}" type="text/javascript"></script>
<script type="text/javascript" src="{{asset('js/frontend/public.js')}}"></script>
<script src="{{asset('js/frontend/gt.js')}}"></script>
<!--<script src="https://ssl.captcha.qq.com/TCaptcha.js"></script>-->
<script>

    var status = 0;
    //手机号码不合格不让点击按钮
    $("#tel").on("input", function() {
        if($(this).val() == "") {
            status = 0;
        } else if($(this).val().length != 11 || isNaN($(this).val()) || !(/^1[3|5|7|8][0-9]\d{4,8}$/.test($(this).val()))) {
            status = 0;
        } else {
            status = 1;
        }
    });

    $(".iosBtn").on('click',function(){
        $(".video").css({display:"block"});
    })
    $(".video-close").on('click',function(){
        $(".video").css({display:"none"});
    })

    //验证码函数
    document.getElementById('getCode').addEventListener('click',function(){
        if(status == 1){
            $("#telErr").text("");
            $.ajax({
                url: "{{url('/api/captcha')}}",
                type: "get",
                data: {},
                dataType: "json",
                success: function (data) {
                    //请检测data的数据结构， 保证data.gt, data.challenge, data.success有值
                    // console.log(data);
                    initGeetest({
                        // 以下配置参数来自服务端 SDK
                        gt: data.gt,
                        challenge: data.challenge,
                        offline: !data.succss,
                        new_captcha: true,
                        product: 'bind',
                    }, handler)
                }
            })
        }else {
            $("#telErr").text("请填写正确的手机号码");
        }
    });
    var handler = function (captchaObj) {
        captchaObj.onReady(function(){
            console.log('验证码加载完成');
            //判断新老用户
            // captchaObj.verify()
            $.ajax({
                url: "{{url('/api/find')}}",
                type: "post",
                data: {
                    phone: $("#tel").val()
                },
                success: function(res) {
                    if(res.data == true) {//已经注册
                        myAlert("该手机已经注册");

                        setTimeout(function() {
                            parent.window.open("https://fir.im/juqinqianbao",'_self');
                        }, 2000);

                    } else { //表示未注册的用户
                        captchaObj.verify()
                    }
                }
            });
        }).onSuccess(function(){
            var result = captchaObj.getValidate();
            console.log(result);
            if(!result) {
                return myAlert('请完成验证');
            }
            readSecond.countdown(function(){
                duanxinyanzheng(result.geetest_challenge,result.geetest_seccode,result.geetest_validate);
            })

        });
        captchaObj.onError(function(){
            myAlert('网络错误，请稍后再试')
        })

    };

    $("#ljsq").on("click", function() {
//        if($('#tel').val()&&$('#code').val()){
        zhuce();
//        }else {
//            myAlert("请先完成手机号、验证码、密码的输入")
//        }
    });

    //发起短信验证请求
    function duanxinyanzheng(challenge, seccode,validate) {
        //极验二次验证及短信验证码获取
        $.ajax({
            url: "{{url('/api/phone/send')}}",
            type: "post",
            data: {
                phone: $("#tel").val()

            },
            success: function(res) {
                if(res.data==true) {
                    myAlert(res.message);

                } else if (res.message == '手机号已被注册') {
                    myAlert(res.message);

                    parent.window.open("https://fir.im/juqinqianbao",'_self');

                } else {
                    myAlert(res.message);
                    // setTimeout(function() {
                    //     location.href = res.data;
                    // }, 1000);
                }
            }
        });
    }
    function GetQueryString(name)
    {
        var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if(r!=null)return  unescape(r[2]); return null;
    }
    //注册
    function zhuce() {
        var err = [];
        if($("#tel").val() == "" || $("#tel").val().length != 11 || isNaN($("#tel").val()) || !(/^1[3|5|7|8][0-9]\d{4,8}$/.test($("#tel").val()))) {
            err.push("请填写正确的手机号码");
            $("#telErr").text("请填写正确的手机号码");
        } else {
            $("#telErr").text("");
        }
        if($("#code").val() == "") {
            err.push("请填写验证码");
            $("#codeErr").text("请填写验证码");
        } else {
            $("#codeErr").text("");
        }
        if($("#pwd").val()=="") {
            err.push("请填写正确的手机号");
            $("#pwdErr").text("请输入密码");
        }
        if(err.length === 0) {
            $.ajax({
                // url: projectName()+"/Api/Account/regist1",
                url: "{{url('/api/register')}}",
                type: "post",
                data: {
                    channel: GetQueryString("code"),
                    sms_code: $("#code").val(),
                    phone: $("#tel").val(), //手机号
                    password: $("#pwd").val(), //密码
                    inviter: $("#inviter").val() //邀请码
                },
                success: function(res) {
                    if(res.code=="200") {
                        myAlert(res.message);
                        setTimeout(function() {
                            parent.window.open("https://fir.im/juqinqianbao",'_self');
                        }, 10000);
                    }else {
                        myAlert(res.message);
                        // setTimeout(function() {
                        //     history.go(0);
                        // }, 2000);
                    }
                }
            });
        }
    }


</script>
<style>
    .geetest_wind.geetest_panel .geetest_panel_box.geetest_panelshowslide{
        width: 500px;
        height: 510px;
    }
</style>

</body>
</html>